package com.daichi.yygh.hosp.controller.api;

import com.daichi.yygh.common.exception.YyghException;
import com.daichi.yygh.common.helper.HttpRequestHelper;
import com.daichi.yygh.common.result.Result;
import com.daichi.yygh.common.result.ResultCodeEnum;
import com.daichi.yygh.common.utils.MD5;
import com.daichi.yygh.hosp.service.DepartmentService;
import com.daichi.yygh.hosp.service.HospitalService;
import com.daichi.yygh.hosp.service.HospitalSetService;
import com.daichi.yygh.hosp.service.ScheduleService;
import com.daichi.yygh.model.hosp.Department;
import com.daichi.yygh.model.hosp.Hospital;
import com.daichi.yygh.model.hosp.Schedule;
import com.daichi.yygh.vo.hosp.DepartmentQueryVo;
import com.daichi.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    private final HospitalService hospitalService;

    private final HospitalSetService hospitalSetService;

    private final DepartmentService departmentService;

    private final ScheduleService scheduleService;

    @Autowired
    public ApiController(HospitalService hospitalService,
                         HospitalSetService hospitalSetService,
                         DepartmentService departmentService,
                         ScheduleService scheduleService) {
        this.hospitalService = hospitalService;
        this.hospitalSetService = hospitalSetService;
        this.departmentService = departmentService;
        this.scheduleService = scheduleService;
    }

    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        checkSignKey(paramMap);

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode((String) paramMap.get("hoscode"));
        scheduleQueryVo.setDepcode((String) paramMap.get("depcode"));

        int page = !StringUtils.hasLength((String) paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = !StringUtils.hasLength((String) paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        Page<Schedule> schedulePage = scheduleService.findSchedulePage(page, limit, scheduleQueryVo);

        return Result.ok(schedulePage);
    }

    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        checkSignKey(paramMap);

        scheduleService.save(paramMap);

        return Result.ok();
    }
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        checkSignKey(paramMap);

        scheduleService.remove((String) paramMap.get("hoscode"), (String) paramMap.get("hosScheduleId"));

        return Result.ok();

    }

    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        checkSignKey(paramMap);

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode((String) paramMap.get("hoscode"));

        int page = !StringUtils.hasLength((String) paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = !StringUtils.hasLength((String) paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        Page<Department> departmentPage = departmentService.findDepartmentPage(page, limit, departmentQueryVo);

        return Result.ok(departmentPage);

    }


    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        checkSignKey(paramMap);

        departmentService.save(paramMap);

        return Result.ok();

    }

    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        checkSignKey(paramMap);

        departmentService.remove((String) paramMap.get("hoscode"), (String) paramMap.get("depcode"));

        return Result.ok();
    }


    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        checkSignKey(paramMap);

        Hospital hospital = hospitalService.getByHoscode((String) paramMap.get("hoscode"));

        return Result.ok(hospital);

    }

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        checkSignKey(paramMap);

        // base64的图片“+”被“ ”替代，还原回来
        String logoData = (String) paramMap.get("logoData");
        paramMap.put("logoData", logoData.replaceAll(" ", "+"));

        hospitalService.save(paramMap);
        return Result.ok();
    }

    private void checkSignKey(Map<String, Object> paramMap) {
        // 传来的sign经过了MD5加密，从数据库查询sign然后MD5后比对
        String sign = (String) paramMap.get("sign");
        String hoscode = (String) paramMap.get("hoscode");

        String signKey = hospitalSetService.getSignKeyByCode(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);

        if (!sign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }


}
